package com.tinyseed.tools.sms.service;

import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.tinyseed.tools.sms.domain.SmsAlarmParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class SmsServiceImpl implements SmsService {

    private final String accessKeyId ="LTAI4Fj1hgwXWhn3R5qGJ5mt";//你的accessKeyId,

    private final String accessKeySecret ="isXkZHsA6VFmSm1zAb9DKGde36naPf";//你的accessKeySecret

    @Override
    public Boolean defaultSendMsg(String phone, SmsAlarmParam smsAlarmParam) {
        boolean bool = false;

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "菏泽京安环保科技有限公司");
        request.putQueryParameter("TemplateCode", "SMS_180049353");

        request.putQueryParameter("TemplateParam", JSONUtil.parse(smsAlarmParam).toString());

        try {
            log.info("调用阿里云短信服务请求 phone={}", phone);

            CommonResponse response = client.getCommonResponse(request);
            log.info(response.getData());
            Map map = JSONUtil.parseObj(response.getData());

            if ("OK".equals(map.get("Code"))) {
                bool =true;
            }
            bool = true;
        } catch (ServerException e) {
            log.error("短信服务端故障");
        } catch (ClientException e) {
            log.error("短信客户端故障");
        }catch (Exception e) {
            log.error("json转换异常:{}", e.toString());
        }

        return bool;
    }

    @Override
    public void QuerySendDetails() {

    }

    @Override
    public Boolean SendBatchSms() {
        return null;
    }
}
